package dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import model.Alunocbo;
import util.ConexaoBD;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;

public class DAOAlunoCbo extends ModelDAO<Alunocbo>{
	private DAOCbo daoCbo = new DAOCbo();
	private DAOAluno daoAluno = new DAOAluno();
	public DAOAlunoCbo() {
        super(Alunocbo.class);
    }
	
	public void save(Alunocbo ac){
		super.begin();
		super.save(ac);
		super.commit();
	}
	
	public void update(Alunocbo ac){
		super.begin();
		super.update(ac);
		super.commit();
	}
	
	public Alunocbo search(int cod){
		super.begin();
		Alunocbo ac = super.search(cod);
		super.commit();
		return ac;
	}
	
	public List<Alunocbo> listartodos(){
		super.begin();
        List<Alunocbo> list = super.findAll();
        super.commit();
        return list;
    }
	
	public void delete(Alunocbo ac){
		super.begin();
		super.delete(ac.getCodAlunoCBO());
		super.commit();
	}
	
	public List<Alunocbo> listar(Integer codAluno) throws SQLException{
		// conectar banco
		Connection con = ConexaoBD.getConnection();
		PreparedStatement stmt = (PreparedStatement) con.prepareStatement ("select * from alunocbo, aluno where aluno.codAluno="+codAluno+" and alunocbo.codAluno=aluno.codAluno");
		ResultSet rs = stmt.executeQuery();
			
		List<Alunocbo> alunocbo = new ArrayList<Alunocbo>();
			
		while (rs.next()){
		//criando o objeto usuario
		Alunocbo cbo = new Alunocbo();
		Integer codCBO = rs.getInt("codCBO");
		cbo.setCbo(daoCbo.search(codCBO));
		Integer codAl = rs.getInt("codAluno");
		cbo.setAluno(daoAluno.search(codAl));
		cbo.setCodAlunoCBO(rs.getInt("codAlunoCBO"));
		cbo.setTempoExpCBO(rs.getString("tempoExpCBO"));
		
		//adicionando o objeto a lista
		alunocbo.add(cbo);
		}
		return alunocbo;
    }

}
